/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.tces.view;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.tces.controller.UserDAO;
import com.tces.model.User;
import java.util.List;
import java.util.regex.Pattern;
import org.apache.log4j.Logger;

/**
 *
 * @author arnolda
 */
public class UserAction extends ActionSupport {

    private static final Logger log = Logger.getLogger(StudentAction.class);
    private User user;
    private List<User> userList;
    private String oper, id, userFirstName,userLastName, userMiddleName, userIDNumber, collegeCode,roleName, userVisible;
    
    @Override
    public String execute() {
        try {
            user = (User) ActionContext.getContext().getSession().get("user");
             if (user.getRoleName().equalsIgnoreCase("Administrator") || user.getRoleName().equalsIgnoreCase("Guidance Counseling Head")) {
                userList = UserDAO.findAll();
//            } else {
//                userList = ProgramDAO.findAll(user.getCollegeID());
            }
            
        } catch (Exception ex) {
            log.error(ex);
        }
        return SUCCESS;
    }
    public String edit() {
        try {
            user = (User) ActionContext.getContext().getSession().get("user");
            if (!Pattern.matches("\\d{2}-\\d{6}", userIDNumber)) {
                addActionError("Invalid ID Number format.");
                log.error("Invalid ID Number format.");
                return ERROR;
            }
            if (oper.equalsIgnoreCase("add")) {

                if (UserDAO.add(getUser()) > 0) {
                    addActionMessage("Successfull insert of student (" + getUser().getUserIDNumber() + ")");
                    return SUCCESS;
                } else {
                    addActionError("An error occured. Please contact your System Administrator");
                    return ERROR;
                }
            } else if (oper.equalsIgnoreCase("edit")) {
                if (UserDAO.update(getUser()) > 0) {
                    addActionMessage("Successfull update of student(" + getUser().getUserIDNumber() + ")");
                    return SUCCESS;
                } else {
                    addActionError("An error occured. Please contact your System Administrator");
                    return ERROR;
                }
            } else if (oper.equalsIgnoreCase("del")) {
//                if (InstructorDAO.delete(Integer.parseInt(id)) > 0) {
//                    addActionMessage("Successfull deactivation of instructor[ID:" + id + "]");
//                    return SUCCESS;
//                } else {
//                    addActionError("An error occured. Please contact your System Administrator");
//                    return ERROR;
//                }
            } else {
                addActionMessage("Invalid operation!");
                return ERROR;
            }
            return SUCCESS;
        } catch (Exception ex) {
            addActionError(ex.toString());
            log.error(ex);
            return ERROR;
        }
    }

    private User getUser() {
        User usr = new User();
        if (userIDNumber != null) {
            usr.setUserIDNumber(userIDNumber);
        }
        if (userFirstName != null) {
            usr.setUserFirstName(userFirstName);
        }
        if (userMiddleName != null) {
            usr.setUserMiddleName(userMiddleName);
        }
        if (userLastName != null) {
            usr.setUserLastName(userLastName);
        }
        if (userVisible != null) {
            usr.setUserVisible(Integer.valueOf(userVisible));
        }
        if (roleName != null) {
            usr.setRoleID(Integer.valueOf(roleName));
        }
        if (collegeCode != null) {
            usr.setCollegeID(Integer.valueOf(collegeCode));
        }

        if (id != null && !(id.equalsIgnoreCase("_empty"))) {
            log.info("" + id.equalsIgnoreCase("_empty"));
            usr.setUserID(Integer.parseInt(id));
        }

        return usr;
    }

    public String getCollegeCode() {
        return collegeCode;
    }

    public void setCollegeCode(String collegeCode) {
        this.collegeCode = collegeCode;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getOper() {
        return oper;
    }

    public void setOper(String oper) {
        this.oper = oper;
    }

    public String getRoleName() {
        return roleName;
    }

    public void setRoleName(String roleName) {
        this.roleName = roleName;
    }

    public String getUserFirstName() {
        return userFirstName;
    }

    public void setUserFirstName(String userFirstName) {
        this.userFirstName = userFirstName;
    }

    public String getUserIDNumber() {
        return userIDNumber;
    }

    public void setUserIDNumber(String userIDNumber) {
        this.userIDNumber = userIDNumber;
    }

    public String getUserLastName() {
        return userLastName;
    }

    public void setUserLastName(String userLastName) {
        this.userLastName = userLastName;
    }

    public List<User> getUserList() {
        return userList;
    }

    public void setUserList(List<User> userList) {
        this.userList = userList;
    }

    public String getUserMiddleName() {
        return userMiddleName;
    }

    public void setUserMiddleName(String userMiddleName) {
        this.userMiddleName = userMiddleName;
    }

    public String getUserVisible() {
        return userVisible;
    }

    public void setUserVisible(String userVisible) {
        this.userVisible = userVisible;
    }
    
}
